﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Threading;
using VIA.Integration;
using VIA.Integration.Domain;
using VIA.Integration.Presentation;
using VIA.Integration.Presentation.View;
using VIA.Presentation;
using VIA.Presentation.Contracts;
using VIA.Presentation.Models;
using VIA.Presentation.Services;
using VIA.RbacPassport;
using VIA.RbacPassport.Presentation;
using VIA.SiMoJi.Domain.Electronics;
using VIA.SiMoJi.Domain.Inspection;

namespace VIA.Project.Presentation.ViewModel.Inspection
{
    [ViewModel(typeof(IMainView))]
    internal class MainViewModel : ViewModel<IMainView>
    {
        FeatureModule _selectedModule;
        readonly IServiceProvider _service;
        readonly IDispatcherService _dispatcherService;
        private readonly ICurrentUser _currentUser;
        private string _currentUserName;
        private string _currentRecipeName;

        public ICollectionView ModulesView { get; }
        public ObservableCollection<FeatureModule> Modules { get; } = new ObservableCollection<FeatureModule>();
        public FeatureModule SelectedModule
        {
            get => _selectedModule;
            set => SetProperty(ref _selectedModule, value);
        }

        public string CurrentUserName
        {
            get => _currentUserName;
            set => SetProperty(ref _currentUserName, value);
        }

        public bool IsRecipeEnable { get => isRecipeEnable; set => SetProperty(ref isRecipeEnable, value); }
        public bool IsManualMode { get => isManualMode; set => SetProperty(ref isManualMode, value); }

        public string CurrentRecipeName
        {
            get => _currentRecipeName;
            set => SetProperty(ref _currentRecipeName, value);
        }
        RunState _runState = RunState.Stop;
        private bool isRecipeEnable;
        private bool isManualMode = true;

        public RunState RunState
        {
            get => _runState;
            set => SetProperty(ref _runState, value);
        }
        public MainViewModel(IServiceProvider services, IDispatcherService dispatcherService, ICurrentUser currentUser, Inspector inspector)
        {
            _dispatcherService = dispatcherService;
            _currentUser = currentUser;
            _currentUser.UserChanged += CurrentUser_UserChanged;
            inspector.ManualAutoStateChanged += Inspector_ManualAutoStateChanged;
            _service = services;
            Modules.Add(new FeatureModule("主   页", "Home", () => services.GetService<IHomeView>()));
            Modules.Add(new FeatureModule("手动调试", "Cog", () => services.GetService<IEquipmentView>(), () => currentUser.IsGranted(PermissionProvider.ManualDebug)));
            Modules.Add(new FeatureModule("参数配置", "CalculatorVariant", () => services.GetService<IParamView>(), () => currentUser.IsGranted(PermissionProvider.Equipment)));
            //Modules.Add(new FeatureModule("生产记录", "ProductionRecord", () => services.GetService<IProductionRecordView>(), () => currentUser.IsGranted(PermissionProvider.ProductionRecord)));
            Modules.Add(new FeatureModule("操作日志", "TextBoxSearchOutline", () => services.GetService<OperatingRecord.Presentation.IOperatingRecordView>(), () => currentUser.IsGranted(PermissionProvider.OperationLog)));
            Modules.Add(new FeatureModule("用户管理", "AccountBoxMultiple", () => services.GetService<IRbacPassportView>(), () => currentUser.IsGranted(RbacPassportPermissionProvider.User) || currentUser.IsGranted(RbacPassportPermissionProvider.Role)));
            Modules.Add(new FeatureModule("设   置", "Setting", () => services.GetService<ISettingsView>(), () => currentUser.IsGranted(PermissionProvider.Settings)));
            ModulesView = CollectionViewSource.GetDefaultView(Modules);
            ModulesView.Filter = o => o is FeatureModule module && module.IsGranted;
            SelectedModule = Modules.First();
            CurrentUserName = currentUser.Name;
            currentUser.UserChanged += (s, e) => dispatcherService.Invoke(() =>
            {
                ModulesView.Refresh();
                CurrentUserName = currentUser.Name;
            });
            ICurrentRecipe currentRecipe = _service.GetService<ICurrentRecipe>();
            currentRecipe.OnRecipeChanged += (s, e) => _dispatcherService.InvokeAsync(() => CurrentRecipeName = e.NewRecipe.Name);
            CurrentRecipeName = currentRecipe.Name;
            if (inspector != null)
                inspector.StatusChanged += (s, e) => _dispatcherService.InvokeAsync(() => RunState = e.CurrentState);
            Check();
        }

        private void Inspector_ManualAutoStateChanged(object sender, ManualAutoStateChangedEventArgs e)
        {
            IsManualMode = e.State == EquipmentManualAutoState.Manual;
            Check();
        }

        private void CurrentUser_UserChanged(object sender, UserChangedEventArgs e)
        {
            Check();
        }

        private void Check()
        {
            IsRecipeEnable = _currentUser.IsGranted(PermissionProvider.RecipeEnable) && IsManualMode && !Electronic.Default.GetIsAutoRun();

        }
    }

}
